package com.hitnet.hitnet.pic;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.hitnet.hitnet.R;

import java.util.List;

public class SelectedAdapter extends BaseAdapter {
	private List<String> lt;// pictures
	private LayoutInflater inflater;
	private GridView selectedGridView;
	private Context context;

	public SelectedAdapter(Context context, List<String> lt,
			GridView selectedGridView) {
		this.context = context;
		this.lt = lt;
		this.selectedGridView = selectedGridView;
		inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return lt.size() + 1;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(final int position, View view, ViewGroup parent) {
		String path = "";
        // get file name
		if (position < lt.size()) {
			path = lt.get(position);
		}
        // the views of images
		ViewHolder holder = null;
		if (view == null) {
			view = inflater.inflate(R.layout.showselectedimage, null);
			holder = new ViewHolder();
			holder.image = (ImageView) view.findViewById(R.id.item_grid_image);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.image.setTag(path);
		if (position == lt.size()) {
			//
			holder.image.setImageResource(R.drawable.icon_addpic_unfocused);
			if (position == 9) {
				//
				holder.image.setVisibility(View.GONE);
			}
		} else {
            // get thumbnail of an image
			Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(
					path, holder.image.getWidth(), holder.image.getHeight(),
					new NativeImageLoader.NativeImageCallBack() {

						public void onImageLoader(Bitmap bitmap, String path) {
							ImageView mImageView = (ImageView) selectedGridView
									.findViewWithTag(path);
							if (bitmap != null && mImageView != null) {
								mImageView.setImageBitmap(bitmap);
							}
						}
					});
            // add thumbnail to view holder for displaying image
			if (bitmap != null) {
				holder.image.setImageBitmap(bitmap);
            // display a whiteboard if image is not in cache
			} else {
				holder.image
						.setImageResource(R.drawable.friends_sends_pictures_no);
			}

		}
		return view;
	}

	public class ViewHolder {
		ImageView image;
	}

}
